{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`numpy.asarray`函数类似于`numpy.array`。   \n",
    "该函数对于将python序列转换为`ndarray`非常有用。  \n",
    "\n",
    "`numpy.asarray(a,dtype=None,order=None)`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 序列转换\n",
    "## 将列表转换为ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "li = [1,2,3]\n",
    "a = np.asarray(li)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设置dtype\n",
    "a1_1 = np.asarray(list1,dtype=float)\n",
    "a1_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将元组转换成ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "tup = (1,2,3)\n",
    "a1_2 = np.asarray(tup)\n",
    "a1_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 转换元组列表成ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([(1, 2, 3), (4, 5)], dtype=object)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = [(1,2,3),(4,5)]\n",
    "a1_3 = np.asarray(x)\n",
    "a1_3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy.frombuffer\n",
    "此函数将缓冲区解释为一维数组。暴露缓冲区接口的任何对象都用作参数来返回`ndarray`.   \n",
    "numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "frombuffer将data以流的形式读入转化成ndarray对象。  \n",
    "**参数：**   \n",
    "offset: 指定读取的起始位置，默认为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([b'e', b'l', b'l', b'o', b' ', b'W', b'o', b'r', b'l', b'd'],\n",
       "      dtype='|S1')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# data是字符串的时候，Python3默认str是Unicode类型。所以需要在原str前加上b转成bytestring类型\n",
    "str = b'Hello World'\n",
    "a2 = np.frombuffer(str, dtype='S1',offset=1)\n",
    "a2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy.fromiter\n",
    "此函数从任何可迭代对象构建一个`ndarray`对象，返回一个新的一维数组。  \n",
    "numpy.fromiter(iterable, dtype, count=-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "iterable: 任何可迭代对象；  \n",
    "dtype: 返回数组的数据类型；  \n",
    "count: 需要读取的数据量，默认为`-1`，读取所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2.])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "list1 = range(5)\n",
    "\n",
    "# 使用迭代器创建ndarray\n",
    "x = np.fromiter(list1, dtype=float, count=3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
